package com.xuerhua.yxt.controller.teacher;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.RelationType;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.TeacherInfo;
import com.xuerhua.yxt.reqVo.TeacherReqVO;
import com.xuerhua.yxt.service.ITeacherService;
import com.xuerhua.yxt.utils.FileUtils;
import com.xuerhua.yxt.wechat.model.WechatUserInfo;
import com.xuerhua.yxt.wechat.utils.WechatUtils;

@Controller
public class TeacherBindController extends BaseController{
	
	@Autowired
	private ITeacherService teacherService;

	/**
	 * 老师绑定
	 * @return
	 */
	@RequestMapping(MenuConst.TEACHER_BIND)
	public String teacherBind(Model model) {
		return "teacher/bind";
	}
	
	@RequestMapping(MenuConst.TEACHER_BIND + "/submit")
	@ResponseBody Object teacherBindSubmit(TeacherReqVO vo) {
		try {
			List<TeacherInfo> list = teacherService.selectByParam(vo);
			if(!(list != null && list.size() > 0)) {
				return new RspResult(Status.PWD_ERROR);
			}
			TeacherInfo teacher = list.get(0);
			String openid = getOpenid();
			WechatUserInfo userInfo = WechatUtils.getUserInfo(WechatUtils.getAccessToken(), openid);
			String headImgUrl = FileUtils.writeHttpFile(userInfo.getHeadimgurl(), RelationType.HEAD_IMG_URL.getCode());
			teacher.setHeadImgUrl(headImgUrl);
			teacher.setOpenid(userInfo.getOpenid());
			teacherService.updateById(teacher);
			return new RspResult(Status.SUCCESS, teacher.getName());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new RspResult(Status.FAIL);
	}
	
}
